<template>
  <div class="app-container">
    <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
    <el-table :data="list" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column type="index" v-if="isTableDate"></el-table-column>
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-if="isTableDate"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'

export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      tableData: [],
      tableHeader: [],
      isTableDate: false,
      list: [],
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 10
      },
      userlist: [],
      total: 0
    }
  },
  // created() {

  // },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
      this.isTableDate = true
      this.getList(this.queryInfo.pagesize, this.queryInfo.pagenum)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getList(this.queryInfo.pagesize, this.queryInfo.pagenum)
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getList(this.queryInfo.pagesize, this.queryInfo.pagenum)
    },
    getList(size, num) {
      this.list = []
      let start = size * (num - 1)
      for (var i = 0; i < size; i++) {
        this.list.push(this.tableData[start + i])
      }
      this.total = this.tableData.length
    }
  }
}
</script>
